package com.violet.myorm.core.condition;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Function;

@FunctionalInterface
public interface SerializableFunction<T, R> extends Function<T, R>, Serializable {

    default String getName() {
        try {
            // 获取 Lambda 表达式对应的 SerializedLambda
            Method writeReplace = this.getClass().getDeclaredMethod("writeReplace");
            writeReplace.setAccessible(true);
            SerializedLambda serializedLambda = (SerializedLambda) writeReplace.invoke(this);
            // 获取方法名, 去掉 get, 首字母小写
            String name = serializedLambda.getImplMethodName().replace("get", "");
            return Character.toLowerCase(name.charAt(0)) + name.substring(1);

        }
        catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}
